Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 519)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.37204 11.42828 11.48354 11.53783 11.59114 11.64349 11.69488 11.74531
##   [9] 11.79478 11.84329 11.89086 11.93748 11.98316 12.02790 12.07170 12.11457
##  [17] 12.15652 12.19753 12.23763 12.27680 12.31504 12.35232 12.38863 12.42398
##  [25] 12.45837 12.49178 12.52422 12.55568 12.58617 12.61568 12.64421 12.67175
##  [33] 12.69831 12.72388 12.74848 12.77215 12.79486 12.81663 12.83743 12.85728
##  [41] 12.87615 12.89405 12.91097 12.92691 12.94185 12.95580 12.96875 12.98069
##  [49] 12.99161 13.00152 13.01032 13.01795 13.02444 13.02981 13.03411 13.03737
##  [57] 13.03962 13.04088 13.04120 13.04060 13.03912 13.03679 13.03364 13.02971
##  [65] 13.02362 13.01421 13.00181 12.98673 12.96930 12.94984 12.92868 12.90613
##  [73] 12.88254 12.85821 12.83347 12.80864 12.78406 12.76003 12.73689 12.71497
##  [81] 12.69457 12.67603 12.65967 12.64264 12.62225 12.59908 12.57372 12.54675
##  [89] 12.51876 12.49031 12.46201 12.43442 12.40813 12.38373 12.36180 12.34292
##  [97] 12.32766 12.31455 12.30170 12.28915 12.27692 12.26503 12.25352 12.24241
## [105] 12.23172 12.22149 12.21173 12.20248 12.19375 12.18559 12.17801 12.17103
## [113] 12.16469 12.15943 12.15556 12.15290 12.15130 12.15059 12.15058 12.15113
## [121] 12.15205 12.15319 12.15436 12.15541 12.15616 12.15645 12.15611 12.15542
## [129] 12.15482 12.15436 12.15408 12.15402 12.15423 12.15477 12.15567 12.15699
## [137] 12.15876 12.16104 12.16387 12.16729 12.17137 12.17613 12.18163 12.18791
## [145] 12.19503 12.20302 12.21271 12.22476 12.23892 12.25495 12.27263 12.29170
## [153] 12.31195 12.33311 12.35497 12.37729 12.39982 12.42233 12.44458 12.46635
## [161] 12.48737 12.50744 12.52629 12.54371 12.55944 12.57327 12.58493 12.59664
## [169] 12.61056 12.62643 12.64398 12.66294 12.68305 12.70405 12.72566 12.74762
## [177] 12.76967 12.79154 12.81296 12.83367 12.85340 12.87189 12.88887 12.90407
## [185] 12.91723 12.92808 12.93635 12.94179 12.94412 12.94308 12.93972 12.93527
## [193] 12.92979 12.92330 12.91584 12.90744 12.89814 12.88797 12.87696 12.86516
## [201] 12.85260 12.83931 12.82532 12.81067 12.79283 12.76957 12.74146 12.70904
## [209] 12.67286 12.63348 12.59145 12.54731 12.50162 12.45492 12.40776 12.36070
## [217] 12.31429 12.26908 12.22561 12.18444 12.14612 12.11119 12.08021 12.04906
## [225] 12.01379 11.97520 11.93409 11.89128 11.84755 11.80373 11.76060 11.71899
## [233] 11.67968 11.64348 11.61121 11.58365 11.56162 11.54318 11.52587 11.50967
## [241] 11.49459 11.48062 11.46776 11.45601 11.44535 11.43580 11.42734 11.41997
## [249] 11.41368 11.40849 11.40437 11.40291 11.40535 11.41118 11.41987 11.43091
## [257] 11.44380 11.45802 11.47304 11.48837 11.50349 11.51787 11.53102 11.54240
## [265] 11.55152 11.56081 11.57282 11.58723 11.60370 11.62192 11.64155 11.66225
## [273] 11.68371 11.70560 11.72758 11.74933 11.77051 11.79081 11.80988 11.82740
## [281] 11.84305 11.85855 11.87570 11.89423 11.91387 11.93438 11.95549 11.97694
## [289] 11.99847 12.01982 12.04073 12.06094 12.08019 12.09822 12.11477 12.13038
## [297] 12.14575 12.16090 12.17584 12.19056 12.20507 12.21939 12.23351 12.24745
## [305] 12.26121 12.27480 12.28822 12.30148 12.31459 12.32755 12.34037 12.35306
## [313] 12.36562 12.37806 12.39039 12.40261 12.41497 12.42767 12.44063 12.45377
## [321] 12.46702 12.48030 12.49352 12.50662 12.51951 12.53211 12.54436 12.55616
## [329] 12.56745 12.57814 12.58874 12.59976 12.61112 12.62275 12.63455 12.64647
## [337] 12.65842 12.67032 12.68210 12.69368 12.70498 12.71593 12.72645 12.73646
## [345] 12.74588 12.75465 12.76267 12.76988 12.77619 12.78152 12.78588 12.78937
## [353] 12.79212 12.79420 12.79574 12.79683 12.79757 12.79808 12.79844 12.79878
## [361] 12.79918 12.79976 12.80062 12.80072 12.79915 12.79618 12.79207 12.78710
## [369] 12.78153 12.77563 12.76967 12.76393 12.75867 12.75415 12.75065 12.74844
## [377] 12.74779 12.74747 12.74619 12.74414 12.74149 12.73843 12.73514 12.73181
## [385] 12.72860 12.72571 12.72331 12.72159 12.72072 12.72090 12.72230 12.72513
## [393] 12.72937 12.73479 12.74119 12.74834 12.75605 12.76408 12.77224 12.78031
## [401] 12.78807 12.79532 12.80183 12.80740 12.81181 12.81485 12.81631 12.81720
## [409] 12.81861 12.82042 12.82251 12.82479 12.82713 12.82942 12.83155 12.83342
## [417] 12.83489 12.83587 12.83625 12.83590 12.83472 12.83285 12.83053 12.82780
## [425] 12.82469 12.82125 12.81750 12.81349 12.80924 12.80480 12.80021 12.79548
## [433] 12.79068 12.78582 12.78095 12.77610 12.77131 12.76661 12.76205 12.75765
## [441] 12.75345 12.74949 12.74456 12.73763 12.72904 12.71910 12.70813 12.69647
## [449] 12.68443 12.67234 12.66051 12.64928 12.63897 12.62990 12.62239 12.61676
## [457] 12.61203 12.60701 12.60180 12.59646 12.59106 12.58570 12.58044 12.57536
## [465] 12.57053 12.56603 12.56194 12.55833 12.55529 12.55287 12.55117 12.55025
## [473] 12.55020 12.55109 12.55299 12.55557 12.55844 12.56163 12.56516 12.56905
## [481] 12.57332 12.57799 12.58308 12.58861 12.59461 12.60110 12.60808 12.61560
## [489] 12.62366 12.63222 12.64119 12.65058 12.66040 12.67063 12.68130 12.69238
## [497] 12.70389 12.71583 12.72819 12.74099 12.75421 12.76786 12.78195 12.79649
## [505] 12.81148 12.82694 12.84286 12.85922 12.87604 12.89330 12.91101 12.92915
## [513] 12.94773 12.96675 12.98619 13.00605 13.02634 13.04705 13.06818
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 519)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.92009 10.99176 11.06227 11.13163 11.19981 11.26680 11.33259 11.39717
##   [9] 11.46053 11.52265 11.58352 11.64313 11.70147 11.75853 11.81429 11.86874
##  [17] 11.92187 11.97367 12.02413 12.07323 12.12098 12.16743 12.21257 12.25645
##  [25] 12.29908 12.34048 12.38067 12.41968 12.45753 12.49424 12.52983 12.56432
##  [33] 12.59773 12.63009 12.66114 12.69064 12.71866 12.74524 12.77045 12.79434
##  [41] 12.81698 12.83842 12.85871 12.87792 12.89611 12.91333 12.92964 12.94509
##  [49] 12.95975 12.97368 12.98585 12.99540 13.00258 13.00766 13.01092 13.01261
##  [57] 13.01301 13.01238 13.01099 13.00910 13.00698 13.00490 13.00312 13.00191
##  [65] 13.00039 12.99755 12.99346 12.98820 12.98184 12.97447 12.96616 12.95698
##  [73] 12.94701 12.93633 12.92501 12.91313 12.90077 12.88799 12.87489 12.86153
##  [81] 12.84799 12.83435 12.82068 12.80537 12.78705 12.76619 12.74322 12.71862
##  [89] 12.69282 12.66629 12.63948 12.61284 12.58682 12.56188 12.53847 12.51705
##  [97] 12.49806 12.47864 12.45594 12.43051 12.40286 12.37352 12.34302 12.31189
## [105] 12.28065 12.24984 12.21998 12.19159 12.16521 12.14137 12.12058 12.10338
## [113] 12.09030 12.07896 12.06682 12.05415 12.04119 12.02820 12.01545 12.00319
## [121] 11.99168 11.98117 11.97192 11.96420 11.95825 11.95434 11.95273 11.95322
## [129] 11.95538 11.95910 11.96427 11.97080 11.97856 11.98745 11.99737 12.00821
## [137] 12.01986 12.03221 12.04515 12.05859 12.07240 12.08649 12.10074 12.11505
## [145] 12.12931 12.14341 12.15963 12.18000 12.20412 12.23156 12.26190 12.29472
## [153] 12.32959 12.36611 12.40384 12.44237 12.48127 12.52013 12.55852 12.59603
## [161] 12.63222 12.66669 12.69901 12.72875 12.75551 12.77885 12.79836 12.81667
## [169] 12.83654 12.85774 12.88005 12.90324 12.92710 12.95138 12.97588 13.00036
## [177] 13.02460 13.04837 13.07144 13.09360 13.11462 13.13427 13.15233 13.16857
## [185] 13.18276 13.19469 13.20413 13.21084 13.21461 13.21522 13.21352 13.21058
## [193] 13.20644 13.20116 13.19477 13.18733 13.17888 13.16948 13.15916 13.14798
## [201] 13.13598 13.12322 13.10973 13.09558 13.07889 13.05805 13.03344 13.00546
## [209] 12.97450 12.94095 12.90521 12.86766 12.82869 12.78871 12.74809 12.70723
## [217] 12.66653 12.62637 12.58715 12.54926 12.51308 12.47902 12.44746 12.41469
## [225] 12.37727 12.33602 12.29176 12.24532 12.19751 12.14915 12.10107 12.05407
## [233] 12.00899 11.96664 11.92785 11.89343 11.86420 11.83657 11.80675 11.77527
## [241] 11.74264 11.70938 11.67602 11.64307 11.61106 11.58050 11.55192 11.52585
## [249] 11.50279 11.48327 11.46782 11.45566 11.44559 11.43743 11.43103 11.42623
## [257] 11.42287 11.42078 11.41981 11.41979 11.42056 11.42197 11.42384 11.42603
## [265] 11.42836 11.43249 11.43991 11.45029 11.46325 11.47845 11.49551 11.51409
## [273] 11.53382 11.55435 11.57531 11.59636 11.61713 11.63726 11.65640 11.67418
## [281] 11.69026 11.70709 11.72708 11.74974 11.77461 11.80122 11.82909 11.85775
## [289] 11.88672 11.91555 11.94375 11.97084 11.99637 12.01986 12.04083 12.06098
## [297] 12.08224 12.10448 12.12756 12.15136 12.17573 12.20055 12.22568 12.25099
## [305] 12.27635 12.30162 12.32668 12.35138 12.37560 12.39920 12.42205 12.44402
## [313] 12.46498 12.48478 12.50331 12.52042 12.53645 12.55187 12.56672 12.58107
## [321] 12.59497 12.60848 12.62167 12.63458 12.64728 12.65982 12.67226 12.68467
## [329] 12.69708 12.70958 12.72191 12.73383 12.74536 12.75651 12.76732 12.77780
## [337] 12.78798 12.79787 12.80750 12.81690 12.82608 12.83506 12.84387 12.85253
## [345] 12.86107 12.86949 12.87784 12.88612 12.89436 12.90217 12.90920 12.91556
## [353] 12.92134 12.92664 12.93155 12.93618 12.94062 12.94497 12.94932 12.95378
## [361] 12.95844 12.96340 12.96875 12.97421 12.97942 12.98441 12.98922 12.99388
## [369] 12.99842 13.00286 13.00725 13.01160 13.01595 13.02033 13.02477 13.02930
## [377] 13.03395 13.03882 13.04395 13.04926 13.05471 13.06023 13.06575 13.07121
## [385] 13.07656 13.08172 13.08664 13.09126 13.09552 13.09934 13.10267 13.10620
## [393] 13.11053 13.11550 13.12095 13.12673 13.13267 13.13861 13.14439 13.14986
## [401] 13.15485 13.15919 13.16274 13.16533 13.16679 13.16698 13.16573 13.16334
## [409] 13.16027 13.15660 13.15236 13.14764 13.14248 13.13695 13.13110 13.12501
## [417] 13.11873 13.11232 13.10584 13.09936 13.09293 13.08571 13.07692 13.06672
## [425] 13.05528 13.04275 13.02929 13.01508 13.00026 12.98500 12.96947 12.95381
## [433] 12.93820 12.92279 12.90774 12.89323 12.87940 12.86642 12.85445 12.84365
## [441] 12.83418 12.82621 12.81833 12.80923 12.79913 12.78827 12.77691 12.76527
## [449] 12.75360 12.74215 12.73115 12.72085 12.71148 12.70329 12.69652 12.69141
## [457] 12.68751 12.68418 12.68140 12.67914 12.67737 12.67607 12.67519 12.67472
## [465] 12.67463 12.67488 12.67546 12.67632 12.67745 12.67880 12.68037 12.68210
## [473] 12.68399 12.68599 12.68808 12.69043 12.69319 12.69635 12.69991 12.70384
## [481] 12.70814 12.71280 12.71779 12.72311 12.72874 12.73466 12.74088 12.74737
## [489] 12.75411 12.76118 12.76863 12.77645 12.78463 12.79317 12.80205 12.81126
## [497] 12.82078 12.83062 12.84076 12.85119 12.86190 12.87288 12.88411 12.89559
## [505] 12.90731 12.91927 12.93149 12.94397 12.95672 12.96975 12.98305 12.99665
## [513] 13.01054 13.02473 13.03923 13.05405 13.06919 13.08466 13.10047
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 519)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.88490 10.94237 10.99890 11.05450 11.10915 11.16285 11.21561 11.26741
##   [9] 11.31826 11.36815 11.41708 11.46503 11.51202 11.55804 11.60308 11.64714
##  [17] 11.69021 11.73230 11.77339 11.81349 11.85262 11.89077 11.92797 11.96420
##  [25] 11.99948 12.03380 12.06716 12.09957 12.13102 12.16152 12.19108 12.21968
##  [33] 12.24734 12.27405 12.29969 12.32416 12.34749 12.36972 12.39087 12.41099
##  [41] 12.43010 12.44824 12.46545 12.48175 12.49718 12.51177 12.52556 12.53858
##  [49] 12.55087 12.56245 12.57299 12.58216 12.59007 12.59678 12.60240 12.60700
##  [57] 12.61067 12.61349 12.61556 12.61696 12.61777 12.61808 12.61797 12.61754
##  [65] 12.61569 12.61142 12.60495 12.59652 12.58635 12.57465 12.56167 12.54762
##  [73] 12.53273 12.51722 12.50133 12.48527 12.46927 12.45356 12.43836 12.42390
##  [81] 12.41040 12.39809 12.38720 12.37601 12.36288 12.34811 12.33203 12.31493
##  [89] 12.29714 12.27897 12.26072 12.24270 12.22524 12.20864 12.19321 12.17926
##  [97] 12.16712 12.15599 12.14489 12.13382 12.12280 12.11181 12.10087 12.08998
## [105] 12.07914 12.06836 12.05763 12.04697 12.03637 12.02584 12.01538 12.00500
## [113] 11.99470 11.98402 11.97260 11.96062 11.94825 11.93564 11.92297 11.91040
## [121] 11.89811 11.88625 11.87500 11.86453 11.85499 11.84656 11.83940 11.83232
## [129] 11.82414 11.81506 11.80529 11.79502 11.78447 11.77383 11.76330 11.75308
## [137] 11.74339 11.73442 11.72637 11.71944 11.71385 11.70978 11.70745 11.70705
## [145] 11.70878 11.71286 11.71915 11.72729 11.73713 11.74854 11.76137 11.77546
## [153] 11.79067 11.80687 11.82389 11.84160 11.85984 11.87848 11.89736 11.91635
## [161] 11.93528 11.95403 11.97244 11.99036 12.00765 12.02417 12.03976 12.05712
## [169] 12.07875 12.10420 12.13304 12.16484 12.19916 12.23556 12.27361 12.31286
## [177] 12.35289 12.39326 12.43354 12.47327 12.51204 12.54940 12.58492 12.61816
## [185] 12.64868 12.67606 12.69984 12.71960 12.73491 12.74531 12.75297 12.76025
## [193] 12.76706 12.77331 12.77890 12.78373 12.78772 12.79077 12.79278 12.79366
## [201] 12.79332 12.79167 12.78860 12.78402 12.77568 12.76176 12.74282 12.71940
## [209] 12.69207 12.66138 12.62790 12.59218 12.55477 12.51624 12.47714 12.43803
## [217] 12.39946 12.36199 12.32619 12.29260 12.26178 12.23430 12.21070 12.18774
## [225] 12.16211 12.13431 12.10481 12.07412 12.04273 12.01111 11.97977 11.94919
## [233] 11.91987 11.89229 11.86694 11.84432 11.82491 11.80706 11.78891 11.77062
## [241] 11.75234 11.73426 11.71653 11.69932 11.68278 11.66709 11.65241 11.63890
## [249] 11.62673 11.61606 11.60705 11.59998 11.59486 11.59150 11.58972 11.58931
## [257] 11.59009 11.59186 11.59442 11.59759 11.60117 11.60497 11.60880 11.61245
## [265] 11.61575 11.62006 11.62670 11.63540 11.64588 11.65785 11.67104 11.68517
## [273] 11.69996 11.71512 11.73038 11.74547 11.76009 11.77397 11.78684 11.79840
## [281] 11.80839 11.81778 11.82768 11.83804 11.84878 11.85985 11.87118 11.88271
## [289] 11.89438 11.90612 11.91786 11.92955 11.94113 11.95251 11.96365 11.97539
## [297] 11.98849 12.00282 12.01823 12.03455 12.05166 12.06939 12.08759 12.10613
## [305] 12.12484 12.14358 12.16220 12.18054 12.19847 12.21583 12.23247 12.24824
## [313] 12.26299 12.27658 12.28885 12.29965 12.30959 12.31935 12.32889 12.33820
## [321] 12.34725 12.35601 12.36445 12.37255 12.38027 12.38761 12.39451 12.40097
## [329] 12.40695 12.41242 12.41711 12.42079 12.42358 12.42556 12.42682 12.42746
## [337] 12.42758 12.42728 12.42663 12.42575 12.42472 12.42364 12.42260 12.42170
## [345] 12.42103 12.42069 12.42077 12.42137 12.42258 12.42366 12.42392 12.42348
## [353] 12.42247 12.42102 12.41927 12.41734 12.41537 12.41349 12.41182 12.41051
## [361] 12.40968 12.40946 12.40999 12.41033 12.40961 12.40800 12.40572 12.40295
## [369] 12.39989 12.39673 12.39366 12.39089 12.38860 12.38699 12.38625 12.38659
## [377] 12.38818 12.39110 12.39516 12.40018 12.40601 12.41246 12.41937 12.42656
## [385] 12.43387 12.44113 12.44815 12.45478 12.46084 12.46616 12.47057 12.47521
## [393] 12.48120 12.48832 12.49636 12.50510 12.51432 12.52382 12.53337 12.54277
## [401] 12.55179 12.56022 12.56785 12.57446 12.57984 12.58377 12.58603 12.58736
## [409] 12.58861 12.58975 12.59076 12.59161 12.59228 12.59273 12.59294 12.59289
## [417] 12.59255 12.59189 12.59089 12.58952 12.58775 12.58510 12.58116 12.57607
## [425] 12.56995 12.56293 12.55512 12.54666 12.53767 12.52827 12.51859 12.50875
## [433] 12.49889 12.48912 12.47956 12.47035 12.46161 12.45347 12.44604 12.43945
## [441] 12.43383 12.42931 12.42487 12.41955 12.41350 12.40688 12.39986 12.39261
## [449] 12.38528 12.37803 12.37103 12.36444 12.35843 12.35315 12.34876 12.34544
## [457] 12.34285 12.34054 12.33850 12.33672 12.33519 12.33390 12.33283 12.33199
## [465] 12.33135 12.33091 12.33065 12.33057 12.33065 12.33088 12.33125 12.33176
## [473] 12.33238 12.33311 12.33394 12.33494 12.33617 12.33764 12.33934 12.34126
## [481] 12.34340 12.34576 12.34833 12.35111 12.35409 12.35727 12.36064 12.36420
## [489] 12.36795 12.37191 12.37612 12.38056 12.38524 12.39014 12.39525 12.40057
## [497] 12.40609 12.41180 12.41770 12.42377 12.43002 12.43643 12.44299 12.44970
## [505] 12.45658 12.46361 12.47082 12.47819 12.48574 12.49346 12.50137 12.50945
## [513] 12.51773 12.52619 12.53485 12.54371 12.55277 12.56204 12.57151
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")